package com.chujian.Activities;

import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
import android.widget.Toast;

import com.chujian.Adapter.OrderCurrentListAdapter;
import com.chujian.Global.Global;
import com.chujian.Threads.HttpClientPostThread;
import com.chujian.Threads.HttpClientGetThread;
import com.chujian.Utils.Constan;
import com.chujian.Utils.JsonUtil;
import com.chujian.model.Order;
import com.chujian.orderingsystem.R;
import com.google.gson.Gson;

public class OrderHistoryListConsumerActivity extends Activity implements 
OnItemClickListener,OnItemLongClickListener{
	private ListView mListView;
	private List<Order> mList;
	private OrderCurrentListAdapter mAdapter;
	private int deletePosition;
	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			super.handleMessage(msg);
			String str = (String) msg.obj;
			if (str != null) {
				if (str.equals("deleteSuccess")) {
					Toast.makeText(OrderHistoryListConsumerActivity.this,"订单删除成功", Toast.LENGTH_SHORT).show();
					mList.remove(deletePosition);
					mAdapter.notifyDataSetChanged();
					
				} else if (str.equals("deleteFailed")) {
					
					Toast.makeText(OrderHistoryListConsumerActivity.this,"订单删除失败", Toast.LENGTH_SHORT).show();
				} else {
					// Toast.makeText(OrderHistoryListConsumerActivity.this,
					// str, Toast.LENGTH_LONG).show();
					mList = JsonUtil.OrderListFromJson(str);
					mAdapter = new OrderCurrentListAdapter(OrderHistoryListConsumerActivity.this, mList);
					mListView.setAdapter(mAdapter);
				}
			}
		};
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.order_history_list_consumer);
		mListView=(ListView) findViewById(R.id.lv_order_history_list_consumer);
		mListView.setOnItemClickListener(this);
		mListView.setOnItemLongClickListener(this);
		
		String userPhone=Global.loginUser.getUserPhone();
		String url=Constan.BASE_URL+"OrderCurrentListServletConsumer"+"?userPhone="+userPhone+"&orderStatus="+1;
		new HttpClientGetThread(url,mHandler).start();;
	}

	public void IbBack(View v) {

		OrderHistoryListConsumerActivity.this.finish();
	}
	
	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View view,
			int position, long id) {
		// TODO Auto-generated method stub
		confirmDialog(position);
		return true;
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if(requestCode==1000&&resultCode==1001){
			int position=data.getIntExtra("deletePosition", -1);
			if(position>=0){
				mList.remove(position);
				mAdapter.notifyDataSetChanged();
			}
		}
	}
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub
		Intent intent =new Intent();
		Gson gson=new Gson ();
		String jsonString=gson.toJson(mList.get(position));
//		Toast.makeText(OrderHistoryListConsumerActivity.this, jsonString, Toast.LENGTH_SHORT).show();
		intent.putExtra("mOrder", jsonString);
		intent.putExtra("position",position);
		intent.setClass(OrderHistoryListConsumerActivity.this,OrderHistoryDetailConsumerActivity.class);
		startActivityForResult(intent, 1000);
		
		
	}
	private void confirmDialog(final int position) {
        AlertDialog.Builder builder = new AlertDialog.Builder(OrderHistoryListConsumerActivity.this);
        builder.setTitle(mList.get(position).getOrderId()+"");
        builder.setMessage("删除操作是不可逆操作，确认删除吗？");
        builder.setPositiveButton("确认", new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialog, int which){
            	deletePosition=position;
            	String url=Constan.BASE_URL+"OrderDeleteConsumerServlet";
            	new HttpClientPostThread(url, mHandler, mList.get(position).getOrderId()).start();;
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialog, int which){
            	
            }
        });
        builder.show();
	}
	
}
